################################################################################
#
#                           Target Definitions
#
#  Description:
#    This makefile structure assumes that the following variables are defined:
#
#      PRIMARY_FILE = name of the target
#      CSRCS = the name of the C sources
#
#      INCLUDEDIRS = list of directories to include (i.e. -I$(ROOT)/...)
#      LIBS = list of libraries needed to build the target (i.e. -lname)
#      LIBDIRS = list of directories to search for libaries (i.e. -L$(ROOT)/...)
#      COMPILE_DEPENDS = list of dependencies for creating the object
#      LINK_DEPENDS = list of dependencies for linking the target
#
#      TARGET = target of the makefile
#
#    To facilitate the process where there are a large number of libraries,
#    rules files are provided in each libary directory that add their
#    contributions to INCLUDEDIRS, LIBS, LIBDIRS, and DEPENDS
#
################################################################################
# Define the root directory
SERVER_ROOT = ..

OBJDIR = OBJ

# Primary file definition
PRIMARY_FILE = simpleServer

CSRCS = serverMain.c hwInterfaceBase.c userPhyAccess.c
COMPILE_DEPENDS = $(PRIMARY_FILE).make $(SERVER_ROOT)/include/userPhyAccess.h

TARGET = ../bin/$(PRIMARY_FILE)
.DEFAULT_GOAL := $(TARGET)

clean:
	make -C $(SERVER_ROOT)/src -f simpleServerLib.make clean
	rm -f $(OBJS)
	rm -f $(TARGET)
	rm -f $(TARGET).exe

COMPILE_DEPENDS += $(SERVER_ROOT)/src/simpleServerLib.make              \
                   $(SERVER_ROOT)/include/server.h                      \
                   $(SERVER_ROOT)/include/tcpServer.h                   \
                   $(SERVER_ROOT)/include/tcpServerProtocol.h           \
                   $(SERVER_ROOT)/include/tcpServerConfig.h             \
                   $(SERVER_ROOT)/include/sockets.h                     \
                   $(SERVER_ROOT)/include/sockets_platform.h            \
                   $(SERVER_ROOT)/include/hwInterfaceBase.h             \
                   $(SERVER_ROOT)/src/server.c                          \
                   $(SERVER_ROOT)/src/hwInterfaceServer.c               \
                   $(SERVER_ROOT)/src/sockets.c
LINK_DEPENDS += $(SERVER_ROOT)/lib/libsimpleServerLib.a
INCLUDEDIRS += -I$(SERVER_ROOT)/include
LIBDIRS += -L$(SERVER_ROOT)/lib
LIBS += -lsimpleServerLib
ifeq ($(OSTYPE),cygwin)
  LIBS += -lws2_32
  C_USER_FLAGS += -DWIN32
endif

# Define the dependencies for the library
$(SERVER_ROOT)/lib/libsimpleServerLib.a : $(SERVER_ROOT)/src/simpleServerLib.make   \
                   $(SERVER_ROOT)/include/server.h                      \
                   $(SERVER_ROOT)/include/tcpServer.h                   \
                   $(SERVER_ROOT)/include/tcpServerProtocol.h           \
                   $(SERVER_ROOT)/include/tcpServerConfig.h             \
                   $(SERVER_ROOT)/include/sockets.h                     \
                   $(SERVER_ROOT)/include/sockets_platform.h            \
                   $(SERVER_ROOT)/include/hwInterfaceBase.h             \
                   $(SERVER_ROOT)/src/server.c                          \
                   $(SERVER_ROOT)/src/hwInterfaceServer.c               \
                   $(SERVER_ROOT)/src/sockets.c
	make -C $(SERVER_ROOT)/src -f simpleServerLib.make

# Define the make rules for the target
# This should be included in a Makefile after the following have been defined:
#
# TARGET = binary make target
# OBJS = list of .o files (to be made from .c)
# CSRCS = list of source files (.c)
# COMPILE_DEPENDS = list of dependendies for creating the object
# LINK_DEPENDS = list of dependencies for linking the target
# C_USER_FLAGS = optional flags supplied by the user for C compilation
# OBJDIR = target directory in which to place object files
# 
# INCLUDEDIRS = additional list of include directories (preceeded with the -I)
# LIBS = additional list of libraries to include (preceeded with the -l)
# LIBDIRS = additional list of library directories to include (preceeded with the -L)

# define the compiler commands and compile flags for C
CC = aarch64-linux-gnu-gcc
#CC = gcc
CFLAGS = -O3 -c -ansi -pedantic -Wall -fmessage-length=0
LINKER_FLAGS = -static -Wall

# list of all C objects.  OBJDIR is the target directory where
# .o files will be placed.  If it doesn't exist, OBJDIR wil be created.
OBJS := $(addprefix $(OBJDIR)/,$(CSRCS:.c=.o))

$(OBJS): | $(OBJDIR)
$(OBJDIR):
	mkdir $(OBJDIR)

# Rule for compiling c files
$(OBJDIR)/%.o : %.c $(COMPILE_DEPENDS)
	$(CC) $(CFLAGS) $(C_USER_FLAGS) $(INCLUDEDIRS) $(OUTPUT_OPTION) $<


# Rule for making the target
$(TARGET): $(OBJS) $(LINK_DEPENDS)
	if [ ! -d $(dir $@) ] ; then mkdir -p $(dir $@) ; fi
	rm -f $(TARGET)
	$(CC) $(LINKER_FLAGS) $(LIBDIRS) -o $(TARGET) $(OBJS) $(LIBS)
